<?php

class Icustom_IndexController extends Core_Controller_Action_Standard
{
  public function indexAction()
  {
    $this->view->someVar = 'someVal';
  }
  public function membersonlineAction(){
  // Get online users
    $table = Engine_Api::_()->getItemTable('user');
    $onlineTable = Engine_Api::_()->getDbtable('online', 'user');
    
    $tableName = $table->info('name');
    $onlineTableName = $onlineTable->info('name');

    //get users online from chat
    $select1 = $table->select()
      ->from($tableName)
      ->join('visichat_online_users', 'visichat_online_users.visi_online_userid = '.$tableName.'.user_id', null);
	$aUsers = $table->fetchAll($select1);
	
	$ids = array();
    foreach ($aUsers as $user){
    	$ids[] = $user->getIdentity();
    }

    $select = $table->select()
      //->from($onlineTableName, null)
      //->joinLeft($tableName, $onlineTable.'.user_id = '.$tableName.'.user_id', null)
      ->from($tableName)
      ->joinLeft($onlineTableName, $onlineTableName.'.user_id = '.$tableName.'.user_id', null)
	  //->where($tableName.'.user_id IN(?)', $ids)
      //->orWhere($onlineTableName.'.user_id > 0 AND ' .$onlineTableName.'.active > ' .new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 1 MINUTE)').' AND '. $tableName.'.search = 1 AND '.$tableName.'.enabled = 1 AND '.$tableName.'.user_id NOT IN(?)', $ids)// GROUP BY '. $onlineTableName.'.user_id ORDER BY '.$onlineTableName.'.active DESC');
      //->where($onlineTableName.'.active > ?', new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 20 MINUTE)'))
      //->where($tableName.'.search = ?', 1)
      //->where($tableName.'.enabled = ?', 1)
      ->order($onlineTableName.'.active DESC')
      ->group($onlineTableName.'.user_id')
      ;
     if(count($ids)){
     	$select->where($tableName.'.user_id IN(?)', $ids)
      		  ->orWhere($onlineTableName.'.user_id > 0 AND ' .$onlineTableName.'.active > ' .new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 1 MINUTE)').' AND '. $tableName.'.search = 1 AND '.$tableName.'.enabled = 1 AND '.$tableName.'.user_id NOT IN(?)', $ids);// GROUP BY '. $onlineTableName.'.user_id ORDER BY '.$onlineTableName.'.active DESC');
     }else{
     	$select->where($onlineTableName.'.user_id > 0')
     		->where($onlineTableName.'.active > ?', new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 20 MINUTE)'))
      		->where($tableName.'.search = ?', 1)
      		->where($tableName.'.enabled = ?', 1);
     } 
	//echo $select;
    $paginator = Zend_Paginator::factory($select);

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->view->itemCountPerPage = $this->_getParam('itemCountPerPage', 16));
    $paginator->setCurrentPageNumber($this->view->page = $this->_getParam('page', 1));

    // Skip if empty
    //$count = $paginator->getTotalItemCount();    
	$this->view->membercount = $count = count($table->fetchAll($select));
      
    if( $count <= 0 ) {
//      return $this->setNoRender();
    }
    $this->view->paginator = $paginator;

    // Make title
//    $this->_onlineUserCount = $count;
    
//    $element = $this->getElement();
//    $title = $this->view->translate(array($element->getTitle(), $element->getTitle(), $count), $this->view->locale()->toNumber($count));
//    $element->setTitle($title);
//    $element->setParam('disableTranslate', true);

    // Guests online
    $this->view->guestCount = null;
    if( $this->_getParam('showGuests', true) ) {
      $this->view->guestCount = $onlineTable->select()
        ->from($onlineTable, new Zend_Db_Expr('COUNT(*) as count'))
        ->where('user_id = ?', 0)
        ->where('active > ?', new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 20 MINUTE)'))
        ->query()
        ->fetchColumn();
        ;
    }
  }
}
